# -*- coding: utf-8 -*-
# created on 2016/8/6

from mathsolver.functions.base import *
from mathsolver.functions.hanshu.compute import HanShuNormalize
from mathsolver.functions.root.jiefangchen import JieFangChen
from sympy import Function
import re


# 判断f(x)的奇偶性
class HanShuJiou(BaseFunction):
    def solver(self, *args):
        func = args[0]

        assert not isinstance(func.expression, list)
        if not func.var.is_Symbol:
            stepsolver = HanShuNormalize().solver(func)
            self.append(stepsolver)
            func = stepsolver.output[0]

        # f(-x)
        new_f = func.expression.subs({func.var: -func.var}).expand().simplify()
        if str(new_f) == str(func.expression):
            self.steps.append(["因为f(x) = f(-x)", "函数是偶函数"])
        elif (new_f + func.expression).simplify().is_zero:
            self.steps.append(["因为f(x) = -f(-x)", "函数是奇函数"])
        else:
            self.steps.append(["因为f(x) = -f(-x)", "函数既不是偶函数也不是奇函数"])

        return self


# f(x)是奇函数
class HanShuJiOuIsJi(BaseFunction):
    odd = True

    def solver(self, *args):

        if isinstance(args[0], BaseFuncName):
            func = self.search(args[0].name)
            if func is None:
                func = BaseFunc(args[0].value)

            func_var = func.var
            # f(x+2) = f(-x+2)
            pattern = re.compile('[A-Za-z]')
            sym = sympify(pattern.search(str(func_var)).group())
            neg_func_var = func_var.subs(sym, -sym)

            f = Function(str(func.name))
            if self.odd:
                func.constrain = Eq(f(func_var), -f(neg_func_var))
                self.steps.append(["", "∵ %s 是奇函数，" % args[0].printing()])
                self.steps.append(
                    ["", "∴ %s " % new_latex(func.constrain).replace("{\\left(", "(").replace("\\right)}", ")")])
                self.output.append(func)
                # f(0) = 0
                self.steps.append(["", "且 f(0)=0"])
                self.output.append(BaseSymbolValue({sympify("f(0)"): sympify(0)}))
                self.label.add("奇函数的定义")
            else:
                func.constrain = Eq(f(func_var), f(neg_func_var))
                self.steps.append(["", "∵ %s 是偶函数，" % args[0].printing()])
                self.steps.append(
                    ["", "∴ %s " % new_latex(func.constrain).replace("{\\left(", "(").replace("\\right)}", ")")])
                self.output.append(func)
                self.label.add("偶函数的定义")

        elif isinstance(args[0], BaseFunc):
            func = args[0]
            for i in range(1, 4):
                # f(a)
                f1, f2 = func.expression.subs({func.var: i}), func.expression.subs({func.var: -i})
                try:
                    if self.odd:
                        stepsolver = JieFangChen().solver(BaseEq([f1, -f2]))
                        self.steps.append(["因为函数是奇函数", "f(x) = -f(-x)"])
                        self.label.add("奇函数的定义")
                    else:
                        stepsolver = JieFangChen().solver(BaseEq([f1, f2]))
                        self.steps.append(["因为函数是偶函数", "f(x) = f(-x)"])
                        self.label.add("偶函数的定义")
                    self.append(stepsolver)
                    self.output = stepsolver.output
                    break
                except Exception:
                    pass

        assert self.output, "no solution found"
        return self


class HanShuJiOuIsOu(HanShuJiOuIsJi):
    odd = False


if __name__ == '__main__':
    pass
